const Service = require('egg').Service;
class userService extends Service {
    async uploadImg(img,userid) {
        // console.log(img,userid)
        const sql=`UPDATE user SET user_img="${img}" WHERE id=${userid}`
        const data = await this.app.mysql.query(sql);
        return data;
      }
    async getUserinfo(userid){
        const sql=`select * from user WHERE id=${userid}`
        const data = await this.app.mysql.query(sql);
        // console.log(data)
        return data;
    }
    async keepUserinfo(obj,userid){
        const sql = `UPDATE user SET user_birthday="${obj.birthday}",sex="${obj.sex}" WHERE id=${userid}`
        const data = await this.app.mysql.query(sql);
        return data
    }
    async myOrder(userid,obj){
        let data = {}
        if(obj.state != undefined){
            const sql=`select *from user_car inner join goods on user_car.goods_id=goods.goods_id WHERE userid = ${userid} AND indent_state=${obj.state}`
            data = await this.app.mysql.query(sql);
        }else{
            const sql=`select *from user_car inner join goods on user_car.goods_id=goods.goods_id WHERE userid = ${userid}`
            data = await this.app.mysql.query(sql);
        }
        
        // console.log(data)
        return data;
    }
    async getAccount(userid){
        const sql = `select * from user_account where userid = ${userid}`
        const data  = await this.app.mysql.query(sql);
        return data
    }
    async canclePay(userid,goodsId){
        goodsId.forEach(async (el)=>{
            const sql = `UPDATE user_car SET indent_state=3   WHERE userid=${userid} and goods_id=${el};`
            const data  = await this.app.mysql.query(sql);
        })
        return "成功"
    }
    async completePay(userid,goodsId,payTime){
        goodsId.forEach(async (el)=>{
            const sql = `UPDATE user_car SET indent_state=1,pay_time="${payTime}" WHERE userid=${userid} and goods_id=${el};`
            const data  = await this.app.mysql.query(sql);
        })
        return "成功"
    }
    async modBalance(balance,userid){
        const sql = `UPDATE user_account SET account_money=${balance}   WHERE userid=${userid}`
        const data  = await this.app.mysql.query(sql);
        return "成功"
    }
}
module.exports = userService;